<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
  <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <script src="../node_modules/vue/dist/vue.js"></script>
  <script src="../node_modules/axios/dist/axios.js"></script>
</head>
<body>
<div id="app">
  <h1>vue-resource插件讲解</h1>
  <a href="javascript:" class="btn btn-primary" v-on:click="get">get请求</a>
  <a href="#" class="btn btn-primary" v-on:click="post">post请求</a>
  <a href="#" class="btn btn-primary" v-on:click="http">http请求</a>
  <span>{{msg}}</span>
</div>
<script>
  new Vue({
    el: "#app",
    data: {
      msg: ''
    },
    mounted: function () {
      axios.interceptors.request.use(function (config) {
        return config;
      })
      axios.interceptors.response.use(function (response) {
        return response;
      })
    },
    methods: {
      get: function () {
        axios.get("../package.json", {
          params: {
            userId: "999"
          },
          headers: {
            token: "jack"
          },
          before: function () {
            console.log("before init")
          }
        }).then(res => {
          this.msg = res.data;
        }).catch(function (error) {
          console.log("" + error)
        });
      },
      post: function () {
        axios.post("../package.json", {
          userId: "888"
        }, {
          headers: {
            token: "tom"
          }
        }).then(res => {
          this.msg = res.data;
        }).catch(function (error) {
          console.log("" + error)
        });

      },
      jsonp: function () {
      },
      http: function () {
        axios({
          url: "../package.json",
          method: "get",
          data: {
            userId: "101"
          },
          params: {
            userId: "102"
          },
          headers: {
            token: "http-test"
          }
        }).then(res => {
          this.msg = res.data;
        }).catch(function (error) {
          console.log("" + error)
        });
      }
    }
  })
</script>
</body>
</html>
